package com.gzdict.myapplication.ui

import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.alibaba.fastjson.JSON
import com.gzdict.common.exception.NetConvertException
import com.gzdict.common.net.FastJsonConverter
import com.gzdict.common.utils.LogUtil
import com.gzdict.common.utils.LogUtil.loge
import com.gzdict.common.utils.LogUtil.logi
import com.gzdict.common.utils.Post
import com.gzdict.common.utils.SPKey
import com.gzdict.common.utils.TipUtils.showToast
import com.gzdict.common.utils.getStringSP
import com.gzdict.common.utils.putStringSp
import com.gzdict.common.utils.scopeDialog
import com.gzdict.myapplication.R
import com.gzdict.myapplication.ui.model.LoginModel
import com.gzdict.myapplication.ui.model.ShopModel


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main_layout)

    }

    fun test(view: View) {
        scopeDialog {
            val map = mutableMapOf<String, String>()
            map["username"] = "thinkgem"
            map["password"] = "Zyyfy@1346790"
            val data =  Post<LoginModel>(ApiConst.URL_LOGIN, JSON.toJSONString(map)).await()
            putStringSp(SPKey.KEY_UID,""+data.id)
            putStringSp(SPKey.KEY_TOKEN,data.authentication!!)
            showToast(data.userName+" 登录成功")
            data.logi()
        }

    }

    fun shop(view: View) {
        scopeDialog {
            val map = mutableMapOf<String, String>()
            val id= getStringSP(SPKey.KEY_UID)
            id?.let {
                map["userId"]= it
            }
            val data =  Post<List<ShopModel>>(ApiConst.URL_SHOP_LIST, JSON.toJSONString(map)).await()
            data.logi()
        }
    }
}

